package server

import (
	"server_go/internal/handler"
	"server_go/internal/middleware"
	"server_go/pkg/jwt"
	"server_go/pkg/log"
	"server_go/pkg/server/http"

	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
)

func NewHTTPServer(
	logger *log.Logger,
	conf *viper.Viper,
	jwt *jwt.JWT,
	userHandler *handler.UserHandler,
	adminHandler *handler.AdminHandler,
	usersHandler *handler.UsersHandler,
	tagHandler *handler.TagHandler,
	articleHandler *handler.ArticleHandler,
) *http.Server {
	gin.SetMode(gin.DebugMode)
	s := http.NewServer(
		gin.Default(),
		logger,
		http.WithServerHost(conf.GetString("http.host")),
		http.WithServerPort(conf.GetInt("http.port")),
	)

	s.Use(
		middleware.CORSMiddleware(),
		middleware.ResponseLogMiddleware(logger),
		middleware.RequestLogMiddleware(logger),
	)

	s.POST("/register", userHandler.Register)
	s.POST("/login", adminHandler.Login)
	s.POST("/userslogin", usersHandler.UsersLogin)
	s.POST("/usersregister", usersHandler.UsersRegister)
	s.DELETE("/deleteuser/:userId", adminHandler.DeleteUser)
	s.GET("/getUsers", adminHandler.GetUsers)
	s.PUT("/updatePassword", usersHandler.ChangePassword)
	s.POST("/createTag", tagHandler.CreateTag)
	s.DELETE("/deleteTag/:tagId", tagHandler.DeleteTag)
	s.GET("/getTags", tagHandler.GetTags)
	s.DELETE("/deleteArticle/:articleId", articleHandler.DeleteArticle)
	s.POST("/createArticle", articleHandler.CreateArticle)
	s.GET("/getArticles", articleHandler.GetArticles)
	s.GET("/articles/user/:userId", articleHandler.GetArticlesByUserId)
	s.GET("/articles/tag/:tagId", articleHandler.GetArticlesByTagID)
	return s
}
